﻿using System;
using Microsoft.Maps.MapControl;
using System.Windows.Data;
using System.Device.Location;
namespace Phone7ActionPack.BingMaps.ValueConverters
{
    public class GeoCoordinateMapLocationValueConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            
        

            if(value == null)
                return null;
            var geoCord = value as GeoCoordinate;
            if(parameter!=null)
            if (parameter.ToString() == "xy")
                return String.Format("{0},{1}", geoCord.Latitude, geoCord.Longitude);
            if (geoCord == null)
                throw new InvalidCastException(string.Format(Resources.Strings.ValueIsNotAGeoCoordinate, value.GetType().Name));
            return new Location(geoCord.Latitude, geoCord.Longitude);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            var loc = value as Location;
            if (loc == null)
                throw new InvalidCastException(string.Format(Resources.Strings.ValueIsNotABingMapLocation, value.GetType().Name));
            return new GeoCoordinate(loc.Latitude, loc.Longitude);
        }
    }
}